using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Dogfight2008.Common
{
  public class FixedList<T> : IEnumerable<T>
  {

    List<T> list;

    void Init(IEnumerable<T> elements)
    {
      list = new List<T>(elements);
    }

    public FixedList(IEnumerable<T> elements)
    {
      Init(elements);
    }

    public FixedList(params T[] elements) {
      Init(elements);
    }

    public int Count {
      get
      {
        return list.Count;
      }
    }

    public T this[int index]
    {
      get
      {
        return list[index];
      }
    }

    public FixedList<T> Sublist(int index, int length)
    {
      List<T> tmplist = new List<T>();
      for (int i = 0; i < length; i++)
        tmplist.Add(this[i + index]);
      return new FixedList<T>(tmplist);
    }

    public override bool Equals(object obj)
    {
      FixedList<T> rhs = obj as FixedList<T>;
      if (rhs == null)
        return false;

      if (Count != rhs.Count)
        return false;

      for (int i = 0; i < Count; i++)
        if (!this[i].Equals(rhs[i]))
          return false;

      return true;
    }

    public override int GetHashCode()
    {
      unchecked
      {
        return list.GetHashCode();
      }
    }

    public override string ToString()
    {
      return string.Format("{0} element fixed list", Count);
    }

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
      return list.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return list.GetEnumerator();
    }

    #endregion
  }
}
